package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.BevelBorder;
import java.awt.Component;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;

import algorithm.*;

import display.CubeDisplay;
import display.Display;
import display.DisplayListener;
import display.SquareDisplay;
import file.FileHandler;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JRadioButtonMenuItem;

/**
 * @author gabriella
 *
 */
public class SejtAutomata3DFrame implements DisplayListener {

	private JFrame frmdSejtautomata;
	private JTextField textFieldC;
	private JTextField textFieldD;
	private JTextField textFieldF;
	private JTextField textFieldP;
	private int T,R,S,P;
	private String TString, RString, SString, PString;
	private JTextField textFieldR;
	private JTextField textFieldS;
	private JTextField textFieldT;
	private JTextField	textFieldSzp;
	private JMenuBar menuBar;
	private JMenu mnFajl;
	private JMenuItem mntmUj;
	private JMenuItem mntmMentes;
	private JMenu mnSugo;
	private JMenuItem mntmSugo;
	private JMenuItem mntmNevjegy;
	
	private JButton startButton;
	
	private JPanel panelOutput;
	private JPanel panelLeft;
	private JPanel panelControl;
	private JPanel panelShowContainer;
	private JPanel panelShow;
	private JPanel statusPanel;
	
	private JLabel lblStatus;
	private JLabel lblSzp;
	
	private JComboBox comboSzabaly;
	
	private JSpinner spinnerGeneracio;
	private SpinnerModel modelGeneracio;
	private JSpinner spinnerMeret;
	private SpinnerModel modelMeret;
	private JSpinner spinnerSzunet;
	private SpinnerModel modelSzunet;
	
	private SejtAutomataHelp Help;
	private String comboText[];
	
	private Display disp = null;
	private Algorithm rule;
	
	private boolean running;
	private JMenu mnNezet;
	private JMenu mnOpciok;
	private JCheckBoxMenuItem chckbxmntmKetszin;
	private JCheckBoxMenuItem chckbxmntmPeridikus;
	private JMenuItem mntmStatisztikaMentse;
	
	private FileHandler fh = null;
	private JRadioButtonMenuItem rdbtnmntmd2D;
	private JRadioButtonMenuItem rdbtnmntmd3D;
	private ButtonGroup rdbgp;
	private JSeparator separator_1;
	private JMenuItem mntmKilps;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SejtAutomata3DFrame window = new SejtAutomata3DFrame();
					window.frmdSejtautomata.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	/**
	 * Checks whether the simulation is ready;
	 * @return true if the simulation is ready, false otherwise
	 */
	public boolean isReady() {
		boolean ready = false;
		if (disp == null) ready = true;
		else ready = disp.isReady();
		return ready;
	}
	
	/**
	 * Disable the components
	 */
	public void setInactive() {

		textFieldR.setEditable(false);
		textFieldS.setEditable(false);
		textFieldT.setEditable(false);
		textFieldP.setEditable(false);
		spinnerSzunet.setEnabled(false);
		spinnerMeret.setEnabled(false);
		spinnerGeneracio.setEnabled(false);
		comboSzabaly.setEnabled(false);
		chckbxmntmKetszin.setEnabled(false);
		chckbxmntmPeridikus.setEnabled(false);
		textFieldSzp.setEditable(false);
		rdbtnmntmd2D.setEnabled(false);
		rdbtnmntmd3D.setEnabled(false);
	}
	/**
	 * Activate the components
	 */
	public void setActive(){
		
		textFieldR.setEditable(true);
		textFieldS.setEditable(true);
		textFieldT.setEditable(true);
		textFieldP.setEditable(true);
		spinnerSzunet.setEnabled(true);
		spinnerMeret.setEnabled(true);
		spinnerGeneracio.setEnabled(true);
		comboSzabaly.setEnabled(true);
		chckbxmntmKetszin.setEnabled(true);
		chckbxmntmPeridikus.setEnabled(true);
		textFieldSzp.setEditable(true);
		rdbtnmntmd2D.setEnabled(true);
		rdbtnmntmd3D.setEnabled(true);
	}

	/**
	 * Create the application.
	 */
	public SejtAutomata3DFrame() {
		running = false;
		fh = new FileHandler();
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {

		frmdSejtautomata = new JFrame();
		frmdSejtautomata.getContentPane().addComponentListener(new ComponentAdapter() {
			@Override
			public void componentResized(ComponentEvent e) {
				if (disp != null) {
					disp.setupSize();
//					((Component) disp).repaint();
//					System.out.println(disp);
				}
			}
		});
		frmdSejtautomata.setTitle("3D Sejtautomata");
		frmdSejtautomata.setBounds(100, 100, 750, 400);
		frmdSejtautomata.setMinimumSize(new Dimension(650, 470));
		
		WindowAdapter exitListener = new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                int confirm = JOptionPane.showOptionDialog(frmdSejtautomata,
                        "Ki akar lépni az alkalmazásból?",
                        "Kilépés", JOptionPane.YES_NO_OPTION,
                        JOptionPane.QUESTION_MESSAGE, null, null, null);
                if (confirm == JOptionPane.YES_OPTION) {
                    System.exit(0);
                }
            }
        };
        frmdSejtautomata.addWindowListener(exitListener);
        frmdSejtautomata.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		
		
		menuBar = new JMenuBar();
		frmdSejtautomata.setJMenuBar(menuBar);
		
		mnFajl = new JMenu("Fájl");
		menuBar.add(mnFajl);
		
		mntmUj = new JMenuItem("Új");
		mntmUj.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (disp != null) disp.abort();
			}
		});
		mnFajl.add(mntmUj);
		
		mntmMentes = new JMenuItem("Kép mentése");
		mntmMentes.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (disp != null) {
					fh.saveEPS(disp);
				}
				else JOptionPane.showMessageDialog(frmdSejtautomata,"Nincs mit menteni (előbb el kell indítani a szimulációt)!","EPS mentés",JOptionPane.ERROR_MESSAGE);
			}
		});
		mnFajl.add(mntmMentes);
		
		mntmStatisztikaMentse = new JMenuItem("Statisztika mentése");
		mntmStatisztikaMentse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (disp != null) {
					fh.saveLog(disp);
				}
				else JOptionPane.showMessageDialog(frmdSejtautomata,"Nincs mit menteni (előbb el kell indítani a szimulációt)!","Statisztika mentése",JOptionPane.ERROR_MESSAGE);
			}
		});
		mnFajl.add(mntmStatisztikaMentse);
		
		separator_1 = new JSeparator();
		mnFajl.add(separator_1);
		
		mntmKilps = new JMenuItem("Kilépés");
		mntmKilps.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int confirm = JOptionPane.showOptionDialog(frmdSejtautomata,
                        "Ki akar lépni az alkalmazásból?",
                        "Kilépés", JOptionPane.YES_NO_OPTION,
                        JOptionPane.QUESTION_MESSAGE, null, null, null);
                if (confirm == JOptionPane.YES_OPTION) {
                    System.exit(0);
                }
			}
		});
		mnFajl.add(mntmKilps);
		
		mnNezet = new JMenu("Nézet");
		menuBar.add(mnNezet);
		
		rdbtnmntmd2D = new JRadioButtonMenuItem("2D");
		rdbtnmntmd2D.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frmdSejtautomata.setTitle("2D Sejtautomata"); // set frame title
				((SpinnerNumberModel) modelMeret).setMaximum(350); // new maximum for spinner
				((SpinnerNumberModel) modelMeret).setStepSize(25); // new stepsize
				((SpinnerNumberModel) modelMeret).setValue(100);
				if (disp != null) {
					disp.destroy();
					((JComponent) disp).setVisible(false);
					panelShow.removeAll();
					panelShow.revalidate();
				}
				disp = null;
			}
		});
		mnNezet.add(rdbtnmntmd2D);
		
		rdbtnmntmd3D = new JRadioButtonMenuItem("3D");
		rdbtnmntmd3D.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frmdSejtautomata.setTitle("3D Sejtautomata"); // set frame title
				((SpinnerNumberModel) modelMeret).setMaximum(25); // new maximum for spinner
				((SpinnerNumberModel) modelMeret).setValue(15);
				((SpinnerNumberModel) modelMeret).setStepSize(1); // new stepsize
				if (disp != null) {
					disp.destroy();
					((JComponent) disp).setVisible(false);
					panelShow.removeAll();
					panelShow.revalidate();
				}
				disp = null;
			}
		});
		rdbtnmntmd3D.setSelected(true);
		mnNezet.add(rdbtnmntmd3D);
		
		rdbgp = new ButtonGroup( );

		rdbgp.add(rdbtnmntmd2D);
		rdbgp.add(rdbtnmntmd3D);
		
		mnOpciok = new JMenu("Opciók");
		menuBar.add(mnOpciok);
		
		chckbxmntmKetszin = new JCheckBoxMenuItem("Két szín");
		mnOpciok.add(chckbxmntmKetszin);
		
		chckbxmntmPeridikus = new JCheckBoxMenuItem("Periodikus");
		mnOpciok.add(chckbxmntmPeridikus);
		
		mnSugo = new JMenu("Súgó");
		menuBar.add(mnSugo);
		
		mntmSugo = new JMenuItem("Súgó");
		mnSugo.add(mntmSugo);
		mntmSugo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Help = new SejtAutomataHelp();
				Help.setBounds(110,110,350,400);
//				Help.setResizable(false);
				Help.setVisible(true);
			}
		});
		
		mntmNevjegy = new JMenuItem("Névjegy");
		mntmNevjegy.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Help = new SejtAutomataHelp();
				JOptionPane.showMessageDialog(frmdSejtautomata,Help.about(),"Névjegy",JOptionPane.PLAIN_MESSAGE);
			}
		});
		
		mnSugo.add(mntmNevjegy);
		frmdSejtautomata.getContentPane().setLayout(new BorderLayout(5, 5));
				
		panelOutput = new JPanel();
		frmdSejtautomata.getContentPane().add(panelOutput, BorderLayout.NORTH);
		panelOutput.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20));
		panelOutput.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		
		JLabel lblC = new JLabel("C = ");
		panelOutput.add(lblC);
		
		textFieldC = new JTextField();
		panelOutput.add(textFieldC);
		textFieldC.setColumns(10);
		textFieldC.setEditable(false);
		
		JLabel lblD = new JLabel("D = ");
		panelOutput.add(lblD);
		
		textFieldD = new JTextField();
		panelOutput.add(textFieldD);
		textFieldD.setColumns(10);
		textFieldD.setEditable(false);
		
		JLabel lblF = new JLabel("F = ");
		panelOutput.add(lblF);
		
		textFieldF = new JTextField();
		panelOutput.add(textFieldF);
		textFieldF.setColumns(10);
		textFieldF.setEditable(false);
	
		
		JSeparator separator = new JSeparator();
		separator.setOrientation(SwingConstants.VERTICAL);
		
		panelLeft = new JPanel();
		frmdSejtautomata.getContentPane().add(panelLeft, BorderLayout.WEST);
		panelLeft.setLayout(new BorderLayout(5, 10));
		
		JLabel lblParamterek = new JLabel("  Paraméterek:");
		panelLeft.add(lblParamterek, BorderLayout.NORTH);
		
		panelControl = new JPanel();
		panelControl.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		
		textFieldR = new JTextField();
		textFieldR.setHorizontalAlignment(JTextField.RIGHT);
		textFieldR.setText("8");
		
		textFieldS = new JTextField();
		textFieldS.setHorizontalAlignment(JTextField.RIGHT);
		textFieldS.setText("6");
		
		textFieldT = new JTextField();
		textFieldT.setHorizontalAlignment(JTextField.RIGHT);
		textFieldT.setText("9");
		
		textFieldP = new JTextField();
		textFieldP.setHorizontalAlignment(JTextField.RIGHT);
		textFieldP.setText("0");
		
		textFieldSzp = new JTextField();
		textFieldSzp.setHorizontalAlignment(JTextField.RIGHT);
		textFieldSzp.setEnabled(false);
		
		
		JLabel lblMeret = new JLabel("Méret =");
		JLabel lblGeneracio = new JLabel("Generáció =");
		JLabel lblR = new JLabel("R =");
		JLabel lblS = new JLabel("S =");
		JLabel lblT = new JLabel("T =");
		JLabel lblP = new JLabel("P =");
		lblSzp = new JLabel("Szp =");
		lblSzp.setEnabled(false);
		JLabel lblSzunet = new JLabel("Szunet (ms) = ");
		JLabel lblSzabaly = new JLabel("Szabály:");
		modelSzunet = new SpinnerNumberModel(100,0,1000,50);
		spinnerSzunet = new JSpinner(modelSzunet);
		modelMeret = new SpinnerNumberModel(15, 3, 25, 1);
		spinnerMeret = new JSpinner(modelMeret);
		modelGeneracio = new SpinnerNumberModel(50,0,500,10);
		spinnerGeneracio = new JSpinner(modelGeneracio);
		
		
		
		comboSzabaly = new JComboBox();
		comboText = new String[4];
		
		comboText[0]="Szabály 1";
		comboText[1]="Szabály 2";
		comboText[2]="Szabály 3";
		comboText[3]="Szabály 4";
		
		for (int i=0;i<4;i++){
			
			comboSzabaly.addItem(comboText[i]);
			
		}
		comboSzabaly.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				String tsz = (String) comboSzabaly.getSelectedItem();
				if (tsz.equals(comboText[0])){
					
					textFieldSzp.setEnabled(false);
					lblSzp.setEnabled(false);
				}
				else {
					
					textFieldSzp.setEnabled(true);
					lblSzp.setEnabled(true);
				
				}
				
			}
		});
		GroupLayout gl_panelControl = new GroupLayout(panelControl);
		gl_panelControl.setAutoCreateGaps(true);
		gl_panelControl.setAutoCreateContainerGaps(true);
		
		
		
		GroupLayout.SequentialGroup hGroup = gl_panelControl.createSequentialGroup();
		hGroup.addGroup(gl_panelControl.createParallelGroup(Alignment.TRAILING)
				.addComponent(lblMeret)
				.addComponent(lblGeneracio)
				.addComponent(lblR)
				.addComponent(lblS)
				.addComponent(lblT)
				.addComponent(lblP)
				.addComponent(lblSzunet)
				.addComponent(lblSzabaly)
				.addComponent(lblSzp));
		hGroup.addGroup(gl_panelControl.createParallelGroup()
				.addComponent(spinnerMeret)
				.addComponent(spinnerGeneracio)
				.addComponent(textFieldR)
				.addComponent(textFieldS)
				.addComponent(textFieldT)
				.addComponent(textFieldP)
				.addComponent(spinnerSzunet)
				.addComponent(comboSzabaly)
				.addComponent(textFieldSzp));
		gl_panelControl.setHorizontalGroup(hGroup);
		
		GroupLayout.SequentialGroup vGroup = gl_panelControl.createSequentialGroup();
		vGroup.addGroup(gl_panelControl.createParallelGroup(Alignment.BASELINE)
				.addComponent(lblMeret).addComponent(spinnerMeret));
		vGroup.addGroup(gl_panelControl.createParallelGroup(Alignment.BASELINE)
				.addComponent(lblGeneracio).addComponent(spinnerGeneracio));
		vGroup.addGroup(gl_panelControl.createParallelGroup(Alignment.BASELINE)
				.addComponent(lblR).addComponent(textFieldR));
		vGroup.addGroup(gl_panelControl.createParallelGroup(Alignment.BASELINE)
				.addComponent(lblS).addComponent(textFieldS));
		vGroup.addGroup(gl_panelControl.createParallelGroup(Alignment.BASELINE)
				.addComponent(lblT).addComponent(textFieldT));
		vGroup.addGroup(gl_panelControl.createParallelGroup(Alignment.BASELINE)
				.addComponent(lblP).addComponent(textFieldP));
		vGroup.addGroup(gl_panelControl.createParallelGroup(Alignment.BASELINE)
				.addComponent(lblSzunet).addComponent(spinnerSzunet));
		vGroup.addGroup(gl_panelControl.createParallelGroup(Alignment.BASELINE)
				.addComponent(lblSzabaly).addComponent(comboSzabaly));
		vGroup.addGroup(gl_panelControl.createParallelGroup(Alignment.BASELINE)
				.addComponent(lblSzp).addComponent(textFieldSzp));
		gl_panelControl.setVerticalGroup(vGroup);
		panelControl.setLayout(gl_panelControl);



		panelLeft.add(panelControl, BorderLayout.CENTER);
		
		Component horizontalStrutEast = Box.createHorizontalStrut(20);
		panelLeft.add(horizontalStrutEast, BorderLayout.EAST);
		
		Component horizontalStrutWest = Box.createHorizontalStrut(20);
		panelLeft.add(horizontalStrutWest, BorderLayout.WEST);
		
		JPanel panelButton = new JPanel();
		panelLeft.add(panelButton, BorderLayout.SOUTH);
		
		
		// the action performed by the start button
		startButton = new JButton("Start");
		startButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				if (running) { // running

					disp.stopSimulation();

				} else { // not running

					if (isReady()) { // have to start over

						fh.resetLog();

						TString = textFieldT.getText();
						RString = textFieldR.getText();
						SString = textFieldS.getText();
						PString = textFieldP.getText();
						boolean szam = true;

						try {
							T = Integer.parseInt(TString);
							R = Integer.parseInt(RString);
							S = Integer.parseInt(SString);
							P = Integer.parseInt(PString);
						} catch (NumberFormatException k) {
							JOptionPane.showMessageDialog(frmdSejtautomata,
									"T, R, S, P négy szám! ", "Start Button",
									JOptionPane.ERROR_MESSAGE);
							szam = false;
							return;
						}
						if ((szam) && (T < 0 || S < 0 || R < 0 || P < 0)) {
							JOptionPane.showMessageDialog(frmdSejtautomata,
									"T, R, S, P négy pozitív szám!",
									"Start Button", JOptionPane.ERROR_MESSAGE);
							return;
						} else if ((szam) && (T > 100) || (R > 100)
								|| (S > 100) || (P > 100)) {
							JOptionPane.showMessageDialog(frmdSejtautomata,
									"T, R, S, P 100-nál kisebb számok!",
									"Start Button", JOptionPane.ERROR_MESSAGE);
							return;

						} else if ((szam)
								&& (((T < R) || (R < S) || (S < P)) || ((T + S) >= 2 * R))) {

							JOptionPane
									.showMessageDialog(
											frmdSejtautomata,
											"Hibas értékek! T > R > S > P és (T + S) < 2*R",
											"Start Button",
											JOptionPane.ERROR_MESSAGE);
							return;
						}
						int meret = (Integer) spinnerMeret.getValue();

						if (comboSzabaly.getSelectedItem().equals(comboText[0])) {
							if (rdbtnmntmd3D.isSelected()) { // 3D Rule1
								rule = new Rule1Cube((Integer) spinnerMeret
										.getValue(), S, R, T, P,
										chckbxmntmPeridikus.getState());
							} else { // 2D Rule1
								rule = new Rule1Square((Integer) spinnerMeret
										.getValue(), S, R, T, P,
										chckbxmntmPeridikus.getState());
							}
						} else if (comboSzabaly.getSelectedItem().equals(
								comboText[1])) {
							int d;
							try {
								d = Integer.parseInt(textFieldSzp.getText());
							} catch (NumberFormatException k) {
								JOptionPane.showMessageDialog(frmdSejtautomata,
										"Szp szám kell legyen! ",
										"Start Button",
										JOptionPane.ERROR_MESSAGE);
								szam = false;
								return;
							}

							if (rdbtnmntmd3D.isSelected()) { // 3D Rule2

								if ((d > 0) && (d <= meret * meret * meret - 1)) {
									rule = new Rule2Cube((Integer) spinnerMeret
											.getValue(), S, R, T, P,
											chckbxmntmPeridikus.getState(), d);
								} else {
									JOptionPane.showMessageDialog(
											frmdSejtautomata,
											"Szp < meret^3 és Szp > 0",
											"Start Button",
											JOptionPane.ERROR_MESSAGE);
									return;
								}
							} else { // 2D Rule2

								if ((d > 0) && (d <= meret * meret - 1)) {
									rule = new Rule2Square(
											(Integer) spinnerMeret.getValue(),
											S, R, T, P, chckbxmntmPeridikus
													.getState(), d);
								} else {
									JOptionPane.showMessageDialog(
											frmdSejtautomata,
											"Szp < meret^2 és Szp > 0",
											"Start Button",
											JOptionPane.ERROR_MESSAGE);
									return;
								}

							}
						} else if (comboSzabaly.getSelectedItem().equals(
								comboText[2])) {
							int d;
							try {
								d = Integer.parseInt(textFieldSzp.getText());
							} catch (NumberFormatException k) {
								JOptionPane.showMessageDialog(frmdSejtautomata,
										"Szp-nek számnak kell lenni",
										"Start Button",
										JOptionPane.ERROR_MESSAGE);
								szam = false;
								return;
							}

							if (rdbtnmntmd3D.isSelected()) { // 3D Rule3

								if ((d > 0) && (d <= meret * meret * meret - 1)) {
									rule = new Rule3Cube((Integer) spinnerMeret
											.getValue(), S, R, T, P,
											chckbxmntmPeridikus.getState(), d);
								} else {
									JOptionPane.showMessageDialog(
											frmdSejtautomata,
											"Szp < meret^3 és Szp > 0",
											"Start Button",
											JOptionPane.ERROR_MESSAGE);
									return;
								}
							} else { // 2D Rule3

								if ((d > 0) && (d <= meret * meret - 1)) {
									rule = new Rule3Square(
											(Integer) spinnerMeret.getValue(),
											S, R, T, P, chckbxmntmPeridikus
													.getState(), d);
								} else {
									JOptionPane.showMessageDialog(
											frmdSejtautomata,
											"Szp < meret^2 és Szp > 0",
											"Start Button",
											JOptionPane.ERROR_MESSAGE);
									return;
								}

							}

						} else {
							int d;
							try {
								d = Integer.parseInt(textFieldSzp.getText());
							} catch (NumberFormatException k) {
								JOptionPane.showMessageDialog(frmdSejtautomata,
										"Szp-nek számnak kell lenni",
										"Start Button",
										JOptionPane.ERROR_MESSAGE);
								szam = false;
								return;
							}

							if (rdbtnmntmd3D.isSelected()) { // 3D Rule4

								if ((d > 0) && (d <= meret * meret * meret - 1)) {
									rule = new Rule4Cube((Integer) spinnerMeret
											.getValue(), S, R, T, P,
											chckbxmntmPeridikus.getState(), d);
								} else {
									JOptionPane.showMessageDialog(
											frmdSejtautomata,
											"Szp < meret^3 és Szp > 0",
											"Start Button",
											JOptionPane.ERROR_MESSAGE);
									return;
								}

							} else { // 2D Rule4

								if ((d > 0) && (d <= meret * meret - 1)) {
									rule = new Rule4Square(
											(Integer) spinnerMeret.getValue(),
											S, R, T, P, chckbxmntmPeridikus
													.getState(), d);
								} else {
									JOptionPane.showMessageDialog(
											frmdSejtautomata,
											"Szp < meret^2 és Szp > 0",
											"Start Button",
											JOptionPane.ERROR_MESSAGE);
									return;
								}

							}

						}
						if (disp != null) {
							disp.destroy();
							panelShow.removeAll();
							panelShow.revalidate();
						}
						if (rdbtnmntmd3D.isSelected()) {

							disp = new CubeDisplay((Integer) spinnerMeret
									.getValue(), rule, (Integer) spinnerSzunet
									.getValue(), (Integer) spinnerGeneracio
									.getValue(), chckbxmntmKetszin.getState(),
									panelShow);
							((JPanel) disp).setBorder(BorderFactory
									.createEmptyBorder(20, 20, 20, 20));
						} else {

							disp = new SquareDisplay((Integer) spinnerMeret
									.getValue(), rule, (Integer) spinnerSzunet
									.getValue(), (Integer) spinnerGeneracio
									.getValue(), chckbxmntmKetszin.getState(),
									panelShow);
							((JPanel) disp).setBorder(BorderFactory
									.createEmptyBorder(20, 20, 20, 20));
						}
						panelShow.add((JPanel) disp, BorderLayout.CENTER);
						((JComponent) disp).setVisible(true);

						SejtAutomata3DFrame.this.setInactive();

						disp.addListener(SejtAutomata3DFrame.this);
						disp.init();

						disp.startSimulation();

						running = true;

						startButton.setText("Stop");

					} else { // just continue

						disp.startSimulation();

						running = true;

						startButton.setText("Stop");

					}
				}
			}
		});
		panelButton.add(startButton);
		
		panelShow = new JPanel();
		panelShowContainer = new JPanel();
		panelShowContainer.setLayout(new BorderLayout());
		panelShowContainer.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		panelShowContainer.add(panelShow, BorderLayout.CENTER);
		panelShow.setBorder(BorderFactory.createMatteBorder(10,10,10,10,panelShow.getBackground()));
		panelShow.setBackground(Color.WHITE);
		frmdSejtautomata.getContentPane().add(panelShowContainer, BorderLayout.CENTER);
		panelShow.setLayout(new BorderLayout(0, 0));
		
		
		statusPanel = new JPanel();
		statusPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
		frmdSejtautomata.getContentPane().add(statusPanel, BorderLayout.SOUTH);
		statusPanel.setPreferredSize(new Dimension(frmdSejtautomata.getContentPane().getWidth(), 20));
		statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.X_AXIS));
		
		lblStatus = new JLabel("  Generáció:");
		lblStatus.setHorizontalAlignment(SwingConstants.LEFT);
		statusPanel.add(lblStatus);
		
		frmdSejtautomata.pack();
		
	}

	@Override
	public void simulationStopped() {
		running = false;
		if (disp.isReady()) { 
			setActive();
			setIterationCount(0);
		}
		else setInactive();
		startButton.setText("Start");
	}

	@Override
	public void setIterationCount(int it) {
		lblStatus.setText("  Generáció: " + it);
	}

	@Override
	public void setPopulationCount(int C, int D) {
		String c = ((Integer) C).toString();
		String d = ((Integer) D).toString();
		float f = ((float) C) /(float) (C + D);
		textFieldC.setText(c);
		textFieldD.setText(d);
		textFieldF.setText(Float.toString(f));
		fh.record(C, D, ((float) C) /(float) (C + D));
	}

}
	